package com.elm.service.impl;

import com.elm.mapper.OrdersMapper;
import com.elm.po.Orders;
import com.elm.po.User;
import com.elm.service.OrdersService;
import com.elm.service.UserService;
import com.elm.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    private String getCurrentUserId() {
        String token = (String) request.getAttribute("token");
        return JwtUtil.getUserIdFromToken(token);
    }

    private String getCurrentUserType() {
        String token = (String) request.getAttribute("token");
        return JwtUtil.getUserTypeFromToken(token);
    }

    @Override
    public int createOrders(Integer businessId, Integer daId, Double orderTotal) {
    	String userId = getCurrentUserId();
        return ordersMapper.createOrders(userId, businessId, daId, orderTotal);
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        return ordersMapper.getOrdersById(orderId);
    }

    @Override
    public List<Orders> getOrdersByUserId() {
    	String currentUserId = getCurrentUserId();
        return ordersMapper.getOrdersByUserId(currentUserId);
    }

    @Override
    public int addComment(Integer orderId, String comment, Integer star) {
    	String currentUserId = getCurrentUserId();
        User user = userService.getUserById(currentUserId);
        if ("banned".equals(user.getStatus())) {
            return 0; // 被封禁用户
        }
        return ordersMapper.addComment(orderId, comment, star, currentUserId);
    }
    
    @Override
	public int updateComment(Integer orderId, String comment, Integer star) {
    	String currentUserId = getCurrentUserId();
        User user = userService.getUserById(currentUserId);
        if ("banned".equals(user.getStatus())) {
            return 0; // 被封禁用户
        }
        return ordersMapper.updateComment(orderId, comment, star, currentUserId);
	}

    @Override
    public List<Orders> getAllCommentsByBusinessId(Integer businessId) {
        return ordersMapper.getAllCommentsByBusinessId(businessId);
    }

    @Override
    public List<Orders> getAllComments() {
    	String currentUserId = getCurrentUserId();
        if (!"admin".equals(getCurrentUserType())) {
            throw new RuntimeException("仅管理员可查看所有评论");
        }
        return ordersMapper.getAllComments(currentUserId);
    }

    @Override
    public int deleteComment(Integer orderId) {
    	String userId = getCurrentUserId();
        if (!"admin".equals(getCurrentUserType())) {
            throw new RuntimeException("仅管理员可删除评论");
        }
        return ordersMapper.deleteComment(orderId, userId);
    }

    @Override
    public List<Orders> getAllOrders(Integer businessId) {
    	String userId = getCurrentUserId();
        User user = userService.getUserById(userId);
        if (!user.getBusinessId().equals(businessId)) {
            throw new RuntimeException("无权限查看其他店铺订单");
        }
        return ordersMapper.getAllOrders(businessId, userId);
    }

    @Override
    public List<Orders> getAllPaidOrders(Integer businessId) {
    	String userId = getCurrentUserId();
        return ordersMapper.getAllPaidOrders(businessId, userId);
    }

    @Override
    public List<Orders> getAllEndedOrders(Integer businessId) {
    	String userId = getCurrentUserId();
        return ordersMapper.getAllEndedOrders(businessId, userId);
    }

    @Override
    public int handleOrders(Integer businessId, Integer orderId) {
    	String userId = getCurrentUserId();
        User user = userService.getUserById(userId);
        if (!user.getBusinessId().equals(businessId)) {
            throw new RuntimeException("无权限处理其他店铺订单");
        }
        return ordersMapper.handleOrders(businessId, orderId, userId);
    }

	
}